﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace GuiFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            txtGui.Focus();
            btnNgat.Enabled = false;
            if (serialPort1.IsOpen)
                serialPort1.Close();
            cmbCong.Items.Clear();
            // Lay cong
            serialPort1.Encoding = Encoding.GetEncoding("UTF-8");
            string[] Port = System.IO.Ports.SerialPort.GetPortNames();
            foreach (string port in Port)
                cmbCong.Items.Add(port);
            cmbCong.SelectedIndex = 0;
        }

        private void btnNgat_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            btnKetNoi.Enabled = true;
            btnNgat.Enabled = false;
            btnGui.Enabled = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult r;
            r = MessageBox.Show("Bạn muốn thoát?", "CẢNH BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (r == DialogResult.No)
                e.Cancel = true;
        }

        private void btnGui_Click(object sender, EventArgs e)
        {
           
            serialPort1.Write(txtGui.Text);
            txtGui.ResetText();
            txtGui.Focus();
        }

        private void btnKetNoi_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.PortName = cmbCong.SelectedItem.ToString();
                serialPort1.Open();
                btnKetNoi.Enabled = false;
                btnNgat.Enabled = true;
                btnGui.Enabled = true;
            }
            catch (Exception)
            {
                MessageBox.Show("Không kết nối được.", "Thử lại", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
          

        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
           string tg =  serialPort1.ReadExisting() + "\n";

           rtbNhan.AppendText(tg);
        }

        private void btnFile_Click(object sender, EventArgs e)
        {
                Stream myStream;
                OpenFileDialog openFileDialog1 = new OpenFileDialog();

                openFileDialog1.InitialDirectory = "c:\\";
                openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                openFileDialog1.FilterIndex = 2;
                openFileDialog1.RestoreDirectory = true;

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        string path = openFileDialog1.FileName;
                        txtGui.Text = path;

                    }
                }
            
        }

        private void cmbCong_SelectedIndexChanged(object sender, EventArgs e)
        {
        
        }

       
    }
}
